<?php
/**
 * Created by PhpStorm.
 * User: sunjian
 * Date: 2018/1/21
 * Time: 11:13
 */

namespace backend\controllers;

use yii\data\Pagination;
use app\models\Area;
use app\models\User;
use yii\web\Controller;
use Yii;

class UserController extends Controller
{
    public function actionOperatorAdd(){
        $areaModel = new Area();
        $areas = $areaModel->find()->where(['flag'=>1])->asArray()->all();
        return $this->render("operator-add",['areas'=>$areas]);
    }

    public function actionOperatorList(){
        $request=Yii::$app->request;

        $query = User::find()->where(['type' => 1]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '20']);
        $operators = $query->offset($pages->offset)
            ->limit($pages->limit)->asArray()
            ->all();

        return $this->render("operator-list",['operators'=>$operators,'p'=>$pages]);
    }
    public function actionAddOperator(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $return = [];
            $user = new User();
            //判断用户名是否存在
            if(User::findOne(['username'=>$request->post("username")])){
                $return['status']=false;
                $return['msg']="用户名已存在";
                return json_encode($return);
            }
            $user->type=1;
            $user->createtime=time();

            $user->username=$request->post("username");
            $user->password=Yii::$app->security->generatePasswordHash($request->post("password"));
            $user->areaid=$request->post("areaid");
            $user->realname=$request->post("realname");
            $user->phone=$request->post("phone");

            if($user->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($user->errors);
            }
            return json_encode($return);
        }

    }

    public function actionOperatorEdit(){
        $request = Yii::$app->request;
        $id=$request->get("id");
        $user=User::findOne($id);
        $areaModel = new Area();
        $areas = $areaModel->find()->where(['flag'=>1])->asArray()->all();
        return $this->render("operator-edit",['user'=>$user,'areas'=>$areas]);
    }

    public function actionOperatorResetPassword(){
        $request = Yii::$app->request;
        $id=$request->get("id");
        $user=User::findOne($id);
        $areaModel = new Area();
        $areas = $areaModel->find()->where(['flag'=>1])->asArray()->all();
        return $this->render("operator-reset-password",['user'=>$user,'areas'=>$areas]);
    }

    public function actionEditOperator(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $return = [];
            $id=$request->post("id");
            $user=User::findOne($id);

            $user->areaid=$request->post("areaid");
            $user->realname=$request->post("realname");
            $user->phone=$request->post("phone");

            if($user->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($user->errors);
            }
            return json_encode($return);
        }

    }

    public function actionResetPasswordOperator(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $return = [];
            $id=$request->post("id");
            $user=User::findOne($id);

            $user->password=Yii::$app->security->generatePasswordHash($request->post("password"));

            if($user->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($user->errors);
            }
            return json_encode($return);
        }

    }

    public function actionDelOperator(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $id= $request->post("id");
            $return = [];
            if(User::findOne($id)->delete()){
                $return['status']=true;
            }else{
                $return['status']=false;

            }
            return json_encode($return);
        }
    }
}